<?php
/**
 * 定时采集58二手车,车主信息导入到车主表里
 */
#error_reporting ( 0 );
//ini_set("display_errors","ON");
//error_reporting(E_ALL ^ E_WARNING);
date_default_timezone_set ( "Asia/Shanghai" );
set_time_limit ( 0 );
if (isset ( $_SERVER ['REQUEST_URI'] )) {
    die ( 'error' );
}
if (! defined ( 'API_ROOT_PATH' )) {
    define ( 'API_ROOT_PATH', dirname ( __FILE__ ) );
}

require_once (API_ROOT_PATH . '/../inc/Config.php');
require_once (API_ROOT_PATH . '/../inc/dblink.php');
require_once (API_ROOT_PATH . '/../inc/Mysql.php');
require_once (API_ROOT_PATH . '/Snoopy.class.php');

global $db;

$db = new Mysql ( MYSQLDB, MYSQLDB_USER, MYSQLDB_PWD, MYSQLDB_DATABASE );
$snoopy = new Snoopy();
$citys = array(
                    'jing'=>'北京',
                    'bj'=>'北京',
                    'lf'=>'廊坊'
                    //三河
                    //固安
                    //涿州
                    //香河
                );
//--------------------------------------------------------------
while ( true ) {

    $city = 'bj';
    $pages = '70';
    $url = "http://".$city.".58.com/ershouche/0/";

    $urls = array($url);
    for($i=1;$i<=$pages;$i++){
        $urls[$i] = $url.'pn'.$i.'/?PGTID=0d30001d-0000-1f7a-'.rand(1000,9999).'-341c7f'.rand(10000,99999).'b&ClickID=2';
    }
    foreach($urls as $key=>$url){
        //echo "获取第".($key+1)."页内的符合条件的URL:".$url."\n";
        $parseurl = parse_url($url);
        $citys=array_flip($citys);
        $city = rtrim($parseurl['host'],'.58.com');
        if(!in_array($city,$citys)){
            //echo "跳过{$url}\n";
            continue;
        };
        //echo "fetching $url \n";
        $snoopy->fetch($url);
        $data2 = $snoopy->results;
        preg_match_all('/<p id="infocont" class="infocont">(.*)<\/p>/isU',$data2,$a2);
        preg_match_all('/<strong class="red">(\d+)<\/strong>/isU',$a2[1][0],$a3);
        if($a3[1][0]=='0'){
            //echo "跳过{$url}\n";
            continue;
        }
        //echo "获取内容页URL\n";
        get_article_link($snoopy,$url);
    }

    //睡觉--------------------------------
    $sleeptimes = getNextSleepSecond();
    print_r("sleep:".$sleeptimes."\n");
    //echo $sql."\n";
    sleep($sleeptimes);
    continue;

}
//-----------------函数-----------------------------------------
//计算下次启动采集时间
function  getNextSleepSecond() {
    $date1 = time();
    $date2 = date('Y-m-d H:i:00',strtotime('+2 hour',$date1));
    $date2 = strtotime($date2);
    $s = $date2-$date1;    
    
    $date1 = date('Y-m-d H:i:s',$date1);
    $date2 = date('Y-m-d H:i:00',$date2);

    print_r("currtime:$date1   nexttime:$date2   timediff:$s\n");

    return $s;

}
//保存数据
function save($data=array()) {
    global $db;
    $data['master_id'] = 0;//主品牌ID
    $data['series_id'] = 0;//系列ID
    $data['status'] = 1;//0无效 1有效
    $data['created_at'] = date('Y-m-d H:i:s');

    $res = false;

    //查重
    $sql = "SELECT owner_co_id FROM au_car_owner_co WHERE phone='".$data['phone']."'";
    $rs = $db->fetchOne($sql);
    if($rs){
        //echo $data['name'].' '.$data['phone'].'已存在于au_car_owner表'."\n";
        return;
    }
    if($data['phone'] && $data['series']){
        try{
            $vals = array();
            foreach($data as $k=>$v){
                array_push($vals,$k.'=\''.addslashes(trim($v)).'\'');
            }
            $insert_sql = "INSERT `au_car_owner_co` SET ".implode(',', $vals);
            $res = $db->query($insert_sql);
        }catch(\Exception $e){
            echo $e->getMessage()."\n";
        }
    }else{
        //echo "\n\n……………………………………\n\n";
        //echo "\$rs[owner_co_id]=".$rs['owner_co_id'] .' \$data[phone]='.$data['phone'].' \$data[series]='. $data['series']."\n";
        //echo "\n\n\n\n";
    }

    if(!$res) {
        //echo $data['name'].' '.$data['phone'].'保存失败'."\n";
    }else{
        //echo $data['name'].' '.$data['phone'].'保存成功'."\n";
    }
}


//列表页面
//获取列表中文章规则
function get_article_link($snoopy,$url){
        global $citys;
        $snoopy->fetch($url);
        $data = $snoopy->results;
        preg_match_all('/<table class="tbimg" cellpadding="0" cellspacing="0">(.*)<div class="pager mb10">/isU',$data,$arr);
        $links = $snoopy->_striplinks($arr[1][0]);
        $links = array_reverse(array_unique($links));
        unset($url);
        foreach($links as $url){

            if(strpos($url,'entinfo=')<=0)continue;

            $parseurl = parse_url($url);
            $citys=array_flip($citys);
            $city = rtrim($parseurl['host'],'.58.com');

            if(!in_array($city,$citys)){
                //echo "跳过{$url}\n";
                continue;
            };
            get_content($snoopy,$url);
        }
}
//获取正文规则
//内容页面
function get_content($snoopy,$url){
    global $citys;
    //echo '开始获取页面内容URL:'.$url."\n";
    $insert = array();
    $insert['comefromurl'] = $url;

    $parseurl = parse_url($url);
    $m_url =  'http://m.58.com/bj/ershouche/'.basename($parseurl['path']);
    $snoopy->fetch($m_url); //获取所有内容
    $data = $snoopy->results;

    //车型,发布时间
    preg_match_all('/<div class="abstract-info">.*<h1 class="abstract-info-title">(.*)<\/h1>.*(\d{4}-\d{2}-\d{2}).*<\/div>/isU',$data,$arr1);
    if($arr1[2][0]!=date('Y-m-d'))return;
    $insert['series'] = $arr1[1][0];
    $insert['comefrom_createtime'] = $arr1[2][0];

    //初登日期，表显里程
    preg_match_all('/<div class="baseinfo-cont">.*<em>(\d{4}-\d{2})<\/em>.*<em>(\d+?\.*?\d*?).*<\/em>.*<\/div>/isU',$data,$arr2);
    $insert['register_at'] = $arr2[1][0].'-01';
    $insert['mileage'] = $arr2[2][0];

    //车主名称,来源
    preg_match_all('/<h3 class="intro-per intro-center">(.*)<span>(.*)<\/span><\/h3>/isU',$data,$arr3);
    $insert['name'] =  $arr3[1][0];
    $source = trim($arr3[2][0]);//个人，商家
    if($source != '个人') return;
    //电话
    preg_match_all('/<span class="meta-phone">(1\d{10})<\/span>/isU',$data,$arr4);
    if(!$arr4[1][0]) return;
    $insert['phone'] = $arr4[1][0];

    $a = explode("&",$url);
    $aa = explode('=',$a[1]);

    $insert['comefromid'] = rtrim($aa[1],'_0');
    $insert['comefrom'] = '1';//来源

    $insert['comefrom_province'] = $citys['bj'];//来源
    $insert['comefrom_city'] = $citys['bj'];//来源

    //入库
    //echo "开始保存采集数据\n";
    if($insert['phone'] && $insert['series']){
        save($insert);
    }else{
        //echo "未匹配到电话信息------------即将采集下一个\n\n\n";
    }
    sleep(1);
}



